<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="菜单名称" prop="name">
      <el-input v-model="dataForm.name" placeholder="菜单名称"></el-input>
    </el-form-item>
    <el-form-item label="功能描述" prop="describe">
      <el-input v-model="dataForm.describe" placeholder="功能描述"></el-input>
    </el-form-item>
    <el-form-item label="是否公开菜单
就是无需分配就可以访问的。所有人可见" prop="isPublic">
      <el-input v-model="dataForm.isPublic" placeholder="是否公开菜单
就是无需分配就可以访问的。所有人可见"></el-input>
    </el-form-item>
    <el-form-item label="对应路由path" prop="path">
      <el-input v-model="dataForm.path" placeholder="对应路由path"></el-input>
    </el-form-item>
    <el-form-item label="对应路由组件component" prop="component">
      <el-input v-model="dataForm.component" placeholder="对应路由组件component"></el-input>
    </el-form-item>
    <el-form-item label="状态" prop="isEnable">
      <el-input v-model="dataForm.isEnable" placeholder="状态"></el-input>
    </el-form-item>
    <el-form-item label="排序" prop="sortValue">
      <el-input v-model="dataForm.sortValue" placeholder="排序"></el-input>
    </el-form-item>
    <el-form-item label="菜单图标" prop="icon">
      <el-input v-model="dataForm.icon" placeholder="菜单图标"></el-input>
    </el-form-item>
    <el-form-item label="菜单分组" prop="group">
      <el-input v-model="dataForm.group" placeholder="菜单分组"></el-input>
    </el-form-item>
    <el-form-item label="父级菜单id" prop="parentId">
      <el-input v-model="dataForm.parentId" placeholder="父级菜单id"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          name: '',
          describe: '',
          isPublic: '',
          path: '',
          component: '',
          isEnable: '',
          sortValue: '',
          icon: '',
          group: '',
          parentId: ''
        },
        dataRule: {
          name: [
            { required: true, message: '菜单名称不能为空', trigger: 'blur' }
          ],
          describe: [
            { required: true, message: '功能描述不能为空', trigger: 'blur' }
          ],
          isPublic: [
            { required: true, message: '是否公开菜单
就是无需分配就可以访问的。所有人可见不能为空', trigger: 'blur' }
          ],
          path: [
            { required: true, message: '对应路由path不能为空', trigger: 'blur' }
          ],
          component: [
            { required: true, message: '对应路由组件component不能为空', trigger: 'blur' }
          ],
          isEnable: [
            { required: true, message: '状态不能为空', trigger: 'blur' }
          ],
          sortValue: [
            { required: true, message: '排序不能为空', trigger: 'blur' }
          ],
          icon: [
            { required: true, message: '菜单图标不能为空', trigger: 'blur' }
          ],
          group: [
            { required: true, message: '菜单分组不能为空', trigger: 'blur' }
          ],
          parentId: [
            { required: true, message: '父级菜单id不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/auth/authmenu/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.name = data.authMenu.name
                this.dataForm.describe = data.authMenu.describe
                this.dataForm.isPublic = data.authMenu.isPublic
                this.dataForm.path = data.authMenu.path
                this.dataForm.component = data.authMenu.component
                this.dataForm.isEnable = data.authMenu.isEnable
                this.dataForm.sortValue = data.authMenu.sortValue
                this.dataForm.icon = data.authMenu.icon
                this.dataForm.group = data.authMenu.group
                this.dataForm.parentId = data.authMenu.parentId
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/auth/authmenu/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'name': this.dataForm.name,
                'describe': this.dataForm.describe,
                'isPublic': this.dataForm.isPublic,
                'path': this.dataForm.path,
                'component': this.dataForm.component,
                'isEnable': this.dataForm.isEnable,
                'sortValue': this.dataForm.sortValue,
                'icon': this.dataForm.icon,
                'group': this.dataForm.group,
                'parentId': this.dataForm.parentId
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
